<?php
namespace Home\Controller;
use Common\Controller\HomeBaseController;
/*前台控制器*/
class IndexController extends HomeBaseController {
  /*首页*/
  public function index(){
    /*Mate信息*/
    $this->assign('meta_title', $this->Config['cmsname']);
    $this->assign('meta_keywords', $this->Config['keywords']);
    $this->assign('meta_description', $this->Config['description']);
    /*模板定义*/
    $this->display();
  }
  /*单页模型*/
  public function pages(){
    /*接收参数*/
    $catid=I('catid');if (empty($catid)){$this->redirect('Index/index');}
    $childs_2 = category_tree($catid);
    //取第一个子catid
    if ($childs_2){
        $child_shift_2 = array_shift($childs_2);
        $catid = $child_shift_2['id'];
    }
    /*栏目信息*/
    $category=$this->CategoryModel->find($catid);
    $this->assign('category',$category);$template=$category['view_tpl'];
    $this->assign('catid',$catid);
    /*单页信息*/
    $pages_row=$this->PagesModel->where(array('catid'=>$catid))->find();
    $this->assign('pages_row',$pages_row);
    $this->assign('content', get_content($pages_row['content']));
    /*Mate信息*/
    if (empty($category['seo_title'])){
      $meta_title=$category['title'];
    }else{
      $meta_title=$category['seo_title'];
    }
    $meta_title = $category['title']. ' - '.$this->Config['cmsname'];
    $meta_keywords =$category['seo_keyword'];
    $meta_description = $category['description'];
    $this->assign('meta_title', $meta_title);
    $this->assign('meta_keywords', $meta_keywords);
    $this->assign('meta_description', $meta_description);
    /*模板定义*/
    $this->display($template);
  }

  /*文章模型*/
  public function Article(){
    /*接收参数*/
    $catid=I('catid'); if (empty($catid)){$this->redirect('Index/index');}
    $childs_2 = category_tree($catid);
    //取第一个子catid
    if ($childs_2){
        $child_shift_2 = array_shift($childs_2);
        $catid = $child_shift_2['id'];
    }
    /*栏目信息*/
    $category=$this->CategoryModel->find($catid);$this->assign('category',$category);
    $this->assign('catid',$catid);$template = $category['lists_tpl'];
    /*信息列表*/
    $map = array('catid'=>$catid,'status'=>1);
    $category['pages_num'] = intval($category['pages_num']);
    if($category['pages_num'] > 0){
      $count=$this->ArticleModel->where($map)->count();
      $Page = new \Think\Page($count,$category['pages_num']);
      $list=$this->ArticleModel->where($map)->order('sort asc,id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
      foreach ($list as $key=>$val){
          $list[$key]['url'] = U('view',array('catid'=>$val['catid'],'id'=>$val['id']));
      }
      $show = $Page->show();$this->assign('page',$show);// 赋值分页输出
    }else{
      $list=$this->ArticleModel->where($map)->order('sort asc,id desc')->select();
      foreach ($list as $key=>$val){
          $list[$key]['url'] = U('view',array('catid'=>$val['catid'],'id'=>$val['id']));
      }
    }
    $this->assign('lists',$list);
    /*Mate信息*/
    $meta_title = $category['title']. ' - '.$this->Config['cmsname'];
    $meta_keywords =$category['seo_keyword'];
    $meta_description = $category['description'];
    $this->assign('meta_title', $meta_title);
    $this->assign('meta_keywords', $meta_keywords);
    $this->assign('meta_description', $meta_description);
    /*模板定义*/
    $this->display($template);
  }


  /*图文模型*/
  public function graphic(){
    /*接收参数*/
    $catid=I('catid'); if (empty($catid)){$this->redirect('Index/index');}
    $childs_2 = category_tree($catid);
    //取第一个子catid
    if ($childs_2){
        $child_shift_2 = array_shift($childs_2);
        $catid = $child_shift_2['id'];
    }
    /*栏目信息*/
    $category=$this->CategoryModel->find($catid);$this->assign('category',$category);
    $this->assign('catid',$catid);$template = $category['lists_tpl'];
    /*信息列表*/
    $map = array('catid'=>$catid,'status'=>1);
    $category['pages_num'] = intval($category['pages_num']);
    if($category['pages_num'] > 0){
      $count=$this->ArticleModel->where($map)->count();
      $Page = new \Think\Page($count,$category['pages_num']);
      $list=$this->ArticleModel->where($map)->order('sort asc,id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
      foreach ($list as $key=>$val){
          $list[$key]['url'] = U('view',array('catid'=>$val['catid'],'id'=>$val['id']));
      }
      $show = $Page->show();$this->assign('page',$show);// 赋值分页输出
    }else{
      $list=$this->ArticleModel->where($map)->order('sort asc,id desc')->select();
      foreach ($list as $key=>$val){
          $list[$key]['url'] = U('view',array('catid'=>$val['catid'],'id'=>$val['id']));
      }
    }
    $this->assign('lists',$list);
    /*Mate信息*/
    $meta_title = $category['title']. ' - '.$this->Config['cmsname'];
    $meta_keywords =$category['seo_keyword'];
    $meta_description = $category['description'];
    $this->assign('meta_title', $meta_title);
    $this->assign('meta_keywords', $meta_keywords);
    $this->assign('meta_description', $meta_description);
    /*模板定义*/
    $this->display($template);
  }



  public function view(){
    /*接收参数*/
    $catid = I('catid', 0, 'intval');$id = I('id', 0, 'intval');
    if (empty($id) || empty($catid)){$this->redirect('Index/index');}
    /*文章信息*/
    $this->ArticleModel->where(array('id' => $id))->setInc('hit',1);
    $info = $this->ArticleModel->find($id);if(empty($info)){$this->redirect('Index/index');}
    $info['content'] = html_entity_decode($info['content']);
    $catid = $info['catid'];
    $this->assign('info',$info);$this->assign('catid',$catid);
    /*栏目信息*/
    $category=$this->CategoryModel->find($catid);$this->assign('category',$category);
    $template = $category['view_tpl'];
    /*上一篇下一篇*/
    $prev=$this->ArticleModel->where("id <".$id.' and catid = '.$catid)->order("sort asc,id desc")->limit("1")->find();
    $p = !$prev ? "没有了" : '<a href="'.U('view',array('catid'=>$prev['catid'],'id'=>$prev['id'])).'">'.$prev['title'].'</a>';
    $this->assign('prev',$p);
    $next=$this->ArticleModel->where("id >".$id.' and catid = '.$catid)->order("sort asc,id asc")->limit("1")->find();
    $n=!$next ? 没有了: '<a href="'.U('view',array('catid'=>$next['catid'],'id'=>$next['id'])).'">'.$next['title'].'</a>';
    $this->assign('next',$n);
    /*Mate信息*/
    $meta_title = $info['title'].' - '.$category['title'].' - ' . $this->Config['cmsname'];
    $meta_keywords = $info['seo_keyword'];
    $meta_description = $info['seo_desc'];
    $this->assign('meta_title', $meta_title);
    $this->assign('meta_keywords', $meta_keywords);
    $this->assign('meta_description', $meta_description);
    $this->display($template);
  }

}
